package com.dd.ent.car.ui.user.uservideo;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.player.MediaPlayerAcceptor;
import com.dd.ent.car.common.player.MediaPlayerManager;
import com.dd.ent.car.common.player.MediaPlayerState;
import com.dd.ent.car.common.player.MediaPlayerStateChangeListener;
import com.dd.ent.car.common.player.ThunderMediaPlayer;
import com.dd.ent.car.common.player.VideoInfo;
import com.dd.ent.car.ui.detail.DetailActivity;
import com.dd.ent.car.ui.detail.DetailFragmentImp;
import com.dd.ent.car.ui.home.templet.HomeBottomView;
import com.dd.ent.car.ui.home.templet.video.VideoItemView;
import com.pp.ent.base.log.MLog;

/**
 * Created by aqu-spring on 16/8/17.
 */
public class UserVideoView extends FrameLayout implements View.OnClickListener ,MediaPlayerAcceptor{
    private VideoItemView videoItemView;
    private HomeBottomView homeBottomView;
    private int position;
    private Video video;
    private ThunderMediaPlayer mThunderMediaPlayer;

    public UserVideoView(Context context) {
        super(context);
        init(context);
    }

    public UserVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_choice_list_video, this, true);
        videoItemView = (VideoItemView) root.findViewById(R.id.video_view);
        homeBottomView = (HomeBottomView) root.findViewById(R.id.video_bottom_view);
        videoItemView.setOnClickListener(this);
        homeBottomView.setBottomItemViewOnClickListener(new HomeBottomView.BottomItemViewOnClickListener() {
            @Override
            public void onLikeClick(View view) {

            }

            @Override
            public void onCommentClick(View view) {
                Context context = getContext();
                if (context == null) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putInt(DetailFragmentImp.HEADER_VIEW_TYPE, DetailFragmentImp.VIDEO_HEADER);
                bundle.putSerializable(DetailFragmentImp.VIDEO, video);
                DetailActivity.startSelf(context, DetailFragmentImp.VIDEO_HEADER,bundle);

            }

            @Override
            public void onShareClick(View view) {

            }
        });
    }

    public void refreshData(int position, Video item) {
        this.position = position;
        video = item;
        videoItemView.refreshData(position, video);
        homeBottomView.refreshData(position, video);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_view:
                doPlay();
                break;
            default:
                break;
        }
    }

    private void doPlay() {
        if (mThunderMediaPlayer != null && mThunderMediaPlayer.getState() != MediaPlayerState.RELEASE) {
            mThunderMediaPlayer = MediaPlayerManager.getInstance().applyMediaPlayer(getContext(), this, mThunderMediaPlayer.getId());
        } else {
            mThunderMediaPlayer = MediaPlayerManager.getInstance().applyMediaPlayer(getContext(), this, MediaPlayerManager.PlayerTag.USER);
        }
        VideoInfo videoInfo = new VideoInfo("12345678",video.getPlayUrl(),video.getTitle());
        mThunderMediaPlayer.play(videoInfo);
    }

    private MediaPlayerStateChangeListener mStateChangeListener = new MediaPlayerStateChangeListener() {
        @Override
        public void onStateChange(MediaPlayerState oldState, MediaPlayerState newState) {
            switch (newState) {
                case INITIALIZED:
                case PREPARING:
                    break;
                case STARTED:
                    break;
            }
        }
    };

    @Override
    public void onPlayerAccept(ThunderMediaPlayer mediaPlayer) {
        mediaPlayer.addContainerTo(videoItemView.getPlayContainer());
        mediaPlayer.addStateChangeListener(mStateChangeListener);
        mediaPlayer.setListPosition(position);
    }

    @Override
    public void onPlayerRelease(ThunderMediaPlayer mediaPlayer) {
        mediaPlayer.removeStateChangeListener(mStateChangeListener);
        mediaPlayer.removeSelfContainer();

    }
}
